﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ERP.Manager.DataEntity;
using ERP.Manager.DataProvider;
using ERP.Manager.DataHelper;
using System.Data.SqlClient;
using System.Data;
using ERP.Manager.Core;
namespace ERP.Manager.SQLServerProvider
{
    public class UserRepository : IUserRepository
    {
        private DepartmentRepository departmentrepository = new DepartmentRepository();

        #region 用户登录的方法
        public UserInfo getUserByName(string userName)
        {
            UserInfo user = null;
            SqlDataReader read = SQLHelper.ExecuteReader(CommandType.Text, ERPSQL.SELECT_LOGIN, new SqlParameter("@userName", userName));
            if (read.Read())
            {
                user = new UserInfo();
                user.id = int.Parse(read["id"].ToString());
                user.userId = read["userId"].ToString();
                user.password = read["password"].ToString();
                user.DepartmentInfo.departmentId = read["departmentId"].ToString();
                user.userName = read["userName"].ToString();
            }
            read.Close();
            read.Dispose();
            return user;
        }
        #endregion

        #region 添加用户信息的方法
        public int addUser(UserInfo user)
        {
            int result = SQLHelper.ExecuteNonQuery(CommandType.Text, ERPSQL.ADD_USER,
                new SqlParameter("@userId", user.userId),
                new SqlParameter("@userName", user.userName),
                new SqlParameter("@sex", user.sex),
                new SqlParameter("@address", user.address),
                new SqlParameter("@age", user.age),
                new SqlParameter("@startingTime", user.startingTime),
                new SqlParameter("@password", user.password),
                new SqlParameter("@departmentId", user.DepartmentInfo.departmentId),
                new SqlParameter("@phoneNumber", user.phoneNumber));
            return result;
        }
        #endregion

        #region 根据编号删除用户信息的方法
        public int DeleteUserinfo(UserInfo user)
        {
            int rowcount = SQLHelper.ExecuteNonQuery(CommandType.Text, ERPSQL.DELETE_USER, new SqlParameter("@userid", user.userId));
            return rowcount;
        }
        #endregion

        #region 修改用户信息的方法
        public int UpdateUserinfo(UserInfo user)
        {
            int result = SQLHelper.ExecuteNonQuery(CommandType.Text, ERPSQL.UPDATE_USER,
                new SqlParameter("@userId", user.userId),
                new SqlParameter("@sex", user.sex),
                new SqlParameter("@age", user.age),
                new SqlParameter("@address", user.address),
                new SqlParameter("@password",user.password),
                new SqlParameter("@departmentId", user.DepartmentInfo.departmentId),
                new SqlParameter("@phoneNumber", user.phoneNumber));
            return result;
        }
        #endregion

        #region 查询所有用户信息
        public List<UserInfo> getAllUsers()
        {
            List<UserInfo> list = new List<UserInfo>();
            UserInfo user = null;
            SqlDataReader read = SQLHelper.ExecuteReader(CommandType.Text, ERPSQL.ALL_USER);
            try
            {
                while (read.Read())
                {
                    user = new UserInfo();
                    user.id = int.Parse(read["id"].ToString());
                    user.userId = read["userId"].ToString();
                    user.userName = read["username"].ToString(); 
                    user.sex = read["sex"].ToString();
                    user.address = read["address"].ToString();                   
                    user.age = int.Parse(read["age"].ToString());
                    user.startingTime = DateTime.Parse(read["startingtime"].ToString());
                    user.password = read["password"].ToString();
                    string departmentId = read["departmentId"].ToString();
                    DepartmentInfo depart = departmentrepository.DetailDepartment(departmentId);
                    user.DepartmentInfo.departmentId = departmentId;
                    user.DepartmentInfo.departmentName = depart.departmentName;
                    user.phoneNumber = read["phonenumber"].ToString();
                    list.Add(user);
                }
                read.Close();
            }
            catch (Exception)
            {
                read.Close();
            }
            finally
            {
                read.Dispose();
            }
            return list;
        }
        #endregion

        #region 根据部门编号查询用户姓名的方法
        public List<UserInfo> ALLDepartmentid_userName(DepartmentInfo depatmentid)
        {
            List<UserInfo> list = new List<UserInfo>();
            SqlDataReader read = SQLHelper.ExecuteReader(CommandType.Text, ERPSQL.ALL_DEPARETMENTID_USER, new SqlParameter("@departmentid", depatmentid.departmentId));
            try
            {
                while (read.Read())
                {
                    UserInfo userinfo = new UserInfo();
                    userinfo.id = int.Parse(read["id"].ToString());
                    userinfo.userId = read["userId"].ToString();
                    userinfo.userName = read["userName"].ToString();
                    userinfo.sex = read["sex"].ToString();
                    userinfo.address = read["address"].ToString();
                    userinfo.age = int.Parse(read["age"].ToString());
                    userinfo.startingTime = DateTime.Parse(read["startingTime"].ToString());
                    userinfo.password = read["password"].ToString();
                    userinfo.DepartmentInfo.departmentId = read["departmentId"].ToString();
                    userinfo.phoneNumber = read["phoneNumber"].ToString();
                    list.Add(userinfo);
                }
                read.Close();
            }
            catch (Exception)
            {
                read.Close();
            }
            finally
            {
                read.Dispose();
            }
            return list;
        }
        #endregion

        #region 分页显示数据的方法
        public List<UserInfo> getAllUsersPagecount(int pagecount)
        {
            List<UserInfo> list = new List<UserInfo>();
            UserInfo user = null;
            SqlDataReader read = SQLHelper.ExecuteReader(CommandType.Text, ERPSQL.ALL_USERS_PAGECOUNT, new SqlParameter("@pagecount", pagecount));
            try
            {
                while (read.Read())
                {
                    user = new UserInfo();
                    user.id = int.Parse(read["id"].ToString());
                    user.userId = read["userId"].ToString();
                    user.userName = read["username"].ToString();
                    user.address = read["address"].ToString();
                    user.sex = read["sex"].ToString();
                    user.age = int.Parse(read["age"].ToString());
                    user.startingTime = DateTime.Parse(read["startingtime"].ToString());
                    DepartmentInfo depart = departmentrepository.DetailDepartment(read["departmentID"].ToString());
                    user.DepartmentInfo.departmentName = depart.departmentName;
                    user.phoneNumber = read["phonenumber"].ToString();
                    list.Add(user);
                }
                read.Close();
            }
            catch (Exception)
            {
                read.Close();
            }
            finally
            {
                read.Dispose();
            }
            return list;
        }
        #endregion
    }
}
